﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Room
{
    class Sessions
    {
        static public int cses = Convert.ToInt32(Config.GetConfig("Net", "cSes")); //max clients count
        // [session] [ip] [hostname] [clientversion]
        static public string[,] sess = new string[cses, 4];

        static public int sIsRangeMas()
        {
            int ind = -1;
            if (cses == 0)
            {
                ConsoleHandle.write(3, "[sSetNullTable] In Table rows = 0. Not edit table.");
                return ind;
            }
            else
            {
                for (int i = 0; i < cses; ++i)
                    if (sess[i, 0] == "(none)")
                    {
                        ind = i;
                        break;
                    }
                return ind;
            }
        }

        static public void sSetNullTable()
        {
            if (cses == 0) ConsoleHandle.write(1, "[sSetNullTable] In Table rows = 0. Not edit table."); //error
            else
            {
                for (int i = 0; i < cses; ++i)
                    for (int j = 0; j < 4; ++j)
                        sess[i, j] = "(none)";
                ConsoleHandle.write(3, "[sSetNullTable] All elements in Table Sessions set Null."); //debug
            }
        }

        static public bool sAddSes(string s, string ip, string hn, string cv)
        {
            if (sIsRangeMas() == -1)
            {
                ConsoleHandle.write(1, "[sAddSes] In array not null elements."); //error
                return false;
            }
            else
            {
                int ind = sIsRangeMas();
                sess[ind, 0] = s;
                sess[ind, 1] = ip;
                sess[ind, 2] = hn;
                sess[ind, 3] = cv;
                ConsoleHandle.write(3, "[sAddSes] Session added."); //debug
                return true;
            }
        }

        static public bool sDelSes(string s)
        {
            if (cses == 0)
            {
                ConsoleHandle.write(3, "[sDelSes] In Table rows = 0. Not edit table.");
                return false;
            }
            else
            {
                int ind = -1;
                for (int i = 0; i < cses; ++i)
                    if (sess[i, 0] == s)
                    {
                        for (int j = 0; j < 4; ++j) sess[i, j] = "(none)";
                        ind = i;
                    }
                if (ind == -1)
                {
                    ConsoleHandle.write(3, "[sDelSes] Session not find in the Table. Not deleted."); //debug
                    return false;
                }
                else
                {
                    ConsoleHandle.write(3, "[sDelSes] Session deleted.");
                    return true;
                }
            }

        }

        static public void sWriteTable()
        {
            ConsoleHandle.write(0, "Таблица сессий (Max count = "+cses.ToString()+"):\n"+
            "[session] [ip] [hostname] [clientversion]"); //info
            for (int i = 0; i < cses; ++i)
            {
                if (sess[i, 0] == "(none)") continue;
                for (int j = 0; j < 4; ++j) Console.Write("[" + sess[i, j] + "] "); //info
                Console.WriteLine();
            }
        }
        
    }
}
